package com.wsjzzcbq.qianyuan.sys.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wsjzzcbq.qianyuan.common.bean.R;
import com.wsjzzcbq.qianyuan.common.cache.CacheService;
import com.wsjzzcbq.qianyuan.common.constant.CacheConsts;
import com.wsjzzcbq.qianyuan.common.constant.SysConsts;
import com.wsjzzcbq.qianyuan.common.util.CookieUtils;
import com.wsjzzcbq.qianyuan.sys.bean.SysUser;
import com.wsjzzcbq.qianyuan.sys.service.SysPermissionService;
import com.wsjzzcbq.qianyuan.sys.service.SysRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * SysPermissionImpl
 *
 * @author wsjz
 * @date 2022/03/10
 */
@Service
public class SysPermissionImpl implements SysPermissionService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private SysRolePermissionService rolePermissionService;

    @Override
    public R<?> menu() {
        String token = CookieUtils.getValyeByName(SysConsts.COOKIE_TOKEN);
        String value = cacheService.get(CacheConsts.userKey(token));
        SysUser sysUser = JSONObject.parseObject(value, SysUser.class);
        //根据角色获取权限
        JSONArray array = rolePermissionService.findMenuByRoleId(sysUser.getRoleId());

        return R.success(array);
    }

}
